import Layout from './Layout.js'
export default class Flow extends Layout {
  constructor(options) {
    super(options)
    this.layoutType = 'flow'
  }
  build() {
    if (this.list.length > 0) {
      let ey = this.list[0].component.top
      for (var i = 0; i < this.list.length; i++) {
        let element = this.list[i]
        let info = this.getInfo(element.component)
        ey = ey + info.dy
        element.update(
          {
            top: ey
          },
          false
        )
        ey = ey + info.dh + element.config.mb || 0
      }
    }
  }
  /**
   * 旋转引发的四个角偏移.
   *
   * @param {} element
   */
  getInfo(element) {
    let { aCoords } = element
    let { bl, br, tl, tr } = aCoords
    // tl 为 left,top

    let min, max
    min = {
      y: aCoords['tl'].y,
      direction: 'tl'
    }
    max = {
      y: aCoords['tl'].y,
      direction: 'tl'
    }
    for (var p in aCoords) {
      let item = aCoords[p]
      if (item.y < min.y) {
        min.y = item.y
        min.direction = p
      }
      if (item.y > max.y) {
        max.y = item.y
        max.direction = p
      }
    }
    let diff_tl_top = aCoords['tl'].y - aCoords[min.direction].y
    let top_to_bottom_height = aCoords[max.direction].y - aCoords['tl'].y
    return {
      dy: diff_tl_top,
      dh: top_to_bottom_height
    }
  }
}
